/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.oa.constants.TaskStatusEnum;
import com.legendshop.oa.dao.TaskDao;
import com.legendshop.oa.dao.TaskEstimateDao;
import com.legendshop.oa.dto.Select2Dto;
import com.legendshop.oa.dto.TaskAssignDto;
import com.legendshop.oa.dto.TaskCancelDto;
import com.legendshop.oa.dto.TaskCloseDto;
import com.legendshop.oa.dto.TaskEstimateDto;
import com.legendshop.oa.dto.TaskFinishDto;
import com.legendshop.oa.dto.TaskQueryDto;
import com.legendshop.oa.dto.TaskStartDto;
import com.legendshop.oa.model.Project;
import com.legendshop.oa.model.Task;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.service.ProjectService;
import com.legendshop.oa.service.TaskService;
import com.legendshop.util.AppUtils;

/**
 * The Class TaskServiceImpl. 服务实现类
 */
public class TaskServiceImpl implements TaskService {
	
	private ProjectService projectService;

	/**
	 *
	 * 引用的Dao接口
	 */
	private TaskDao taskDao;

	private TaskEstimateDao taskEstimateDao;
	
	/**
	 * 根据Id获取
	 */
	public Task getTask(UserDetail userDetail,Long id) {
		Task task = taskDao.getTask(id);
		Project project = projectService.getProject(userDetail, task.getProjectId());//检查任务所属的项目权限权限
		task.setProject(project);
		return task;
	}

	/**
	 * 删除
	 */
	public boolean deleteTask(Task task) {
//		//获取已经审核过得日志, 注意把日志和任务分离,删除任务不要删除日志
//		List<TaskEstimate> list = taskEstimateDao.getReviewedTaskEstimateByTask(task.getId());
//		if(AppUtils.isNotBlank(list)){
//			//存在已经评审过得日志，无法删除
//			return false;
//		}else{
//			taskDao.deleteTask(task);
//			taskEstimateDao.deleteTaskEstimateByTask(task.getId());
//			return true;
//		}
		
		int result = taskDao.deleteTask(task);
		return result > 0;
	}

	/**
	 * 保存
	 */
	public Task saveTask(Task task, UserDetail ud) {
		if (!AppUtils.isBlank(task.getId())) {
			Task origin = taskDao.getTask(task.getId());
			if(origin != null) {
				Date date = new Date();
				origin.setColor(task.getColor());
				
				origin.setLasteditedby(ud.getUsername());//最后的更新人
				origin.setLastediteddate(date);
				origin.setModuleId(task.getModuleId());
				origin.setName(task.getName());
				origin.setType(task.getType());
				origin.setPriority(task.getPriority());
				origin.setEststarted(task.getEststarted());
				origin.setDeadline(task.getDeadline());
				origin.setLeftHours(task.getLeftHours());
				if(task.getAssignedto()!=null) {
					origin.setAssignedto(task.getAssignedto());
					origin.setAssigneddate(date);
				}
				origin.setMailto(task.getMailto());
				origin.setMemo(task.getMemo());
				origin.setPlanId(task.getPlanId());
				updateTask(origin);
				return origin;
			}
			return null;//找不到数据
		}
		
		Date date = new Date();
		task.setCreateTime(date);
		task.setCreateBy(ud.getUsername());
		if(task.getStatus() == null) {
			task.setStatus(TaskStatusEnum.UNSTART.value());
		}
		task.setDeleted(0);//新建还没有删除
		task.setLasteditedby(ud.getUsername());//最后的更新人
		task.setLastediteddate(date);
		
		task.setAssigneddate(date);
		 taskDao.saveTask(task,ud);
		 return task;
	}

	/**
	 * 更新
	 */
	public void updateTask(Task task) {
		taskDao.updateTask(task);
	}

	@Override
	public PageSupport<Task> getTask(TaskQueryDto taskQueryDto) {
		return taskDao.getTask(taskQueryDto);
	}

	@Override
	public boolean assignUpdateTask(Long taskId, TaskAssignDto taskAssignDto, UserDetail ud) {
		return taskDao.assignUpdateTask(taskId,taskAssignDto,ud);
	}

	@Override
	public boolean startUpdateTask(Long taskId, TaskStartDto taskStartDto, UserDetail ud) {
		return taskDao.startUpdateTask(taskId,taskStartDto,ud);

	}

	@Override
	public boolean finishUpdateTask(Long taskId, TaskFinishDto taskFinishDto, UserDetail ud) {
		return taskDao.finishUpdateTask(taskId,taskFinishDto,ud);
	}

	@Override
	public boolean closeUpdateTask(Long taskId, TaskCloseDto taskCloseDto, UserDetail ud) {
		return taskDao.closeUpdateTask(taskId,taskCloseDto,ud);
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	@Override
	public boolean cancelUpdateTask(Long taskId, TaskCancelDto taskCancelDto,UserDetail ud) {
		return taskDao.cancelUpdateTask(taskId,taskCancelDto,ud);
	}

	@Override
	public boolean saveTaskEstimate(Task task, List<TaskEstimateDto> taskEstimateList, UserDetail ud) {
		return taskDao.saveTaskEstimate(task, taskEstimateList, ud);
	}
	

	@Override
	public boolean saveTaskEstimate(Task task, TaskEstimateDto taskEstimate, UserDetail ud) {
		return taskDao.saveTaskEstimate(task, taskEstimate, ud);
	}

	/**
	 * 查询我的任务
	 * @param startDate
	 * @param endDate
	 * @param userName
	 * @return
	 */
	@Override
	public List<Select2Dto> queryMyList(Date startDate, Date endDate, String userName) {
		return taskDao.queryList(null, startDate, endDate, userName, null, 50, 1);
	}


	public void setTaskEstimateDao(TaskEstimateDao taskEstimateDao) {
		this.taskEstimateDao = taskEstimateDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	@Override
	public JSONObject queryList(Long projectId, Date startDate, Date endDate, String userName, String q, Integer pageSize, Integer currPage) {
		List<Select2Dto> list = taskDao.queryList(projectId, startDate, endDate, userName, q, pageSize, currPage);
		int total = taskDao.taskCount(projectId,startDate, endDate, userName, q);
		JSONObject result = new JSONObject();
		result.put("results", list);
		result.put("total", total);
		return result;
	}

}
